#!/usr/bin/perl

require "accounts.pl";
&ReadParse();

my (%ogroup, %group, $error_msg, $local_members, $not_local_members, $umembers, $groupname, $gid);
my ($entry_local_members, @line);

$error_msg = "";

if($in{"groupname"})
{
	$groupname = $in{"groupname"};
}
else
{
	$error_msg = $text{'error_msg_no_groupname'};
}

unless($error_msg)
{
	$error_msg = &check_simbol_uname($groupname);
	$logminor= $text{'module_name'};
	$logpriority=$text{'naslog_warning'};
	$event = $text{'groups_naslog_event_modify_fail_gname_err_format'};
	$formatstr = sprintf $text{'groups_naslog_modify_fail_gname_err_format'}, $groupname;
}

unless($error_msg)
{
	unless($groupname eq $in{"oldname"})
	{
		$gid = &local_group_exist($groupname);
		if($gid >= 0)
		{
			$error_msg = $text{'error_msg_group_exist_local'};
			$logminor= $text{'module_name'};
			$logpriority=$text{'naslog_warning'};
			$event = $text{'groups_naslog_event_modify_fail_gname_exist_local'};
			$formatstr = sprintf $text{'groups_naslog_modify_fail_gname_exist_local'}, $groupname;	
		}
	}
}

unless($error_msg)
{
	unless($groupname eq $in{"oldname"})
	{
		$gid = &find_gid_by_gname($groupname);
		if($gid >= 0)
		{
			$error_msg = $text{'error_msg_group_exist_sys'};
			$logminor= $text{'module_name'};
			$logpriority=$text{'naslog_warning'};
			$event = $text{'groups_naslog_event_modify_fail_gname_exist_sys'};
			$formatstr = sprintf $text{'groups_naslog_modify_fail_gname_exist_sys'}, $groupname;
		}
	}
}

unless($error_msg)
{
	$error_msg = "";

	$ogroup{"group"} = $in{"oldname"};
	&get_group_detail(\%ogroup);

	$entry_local_members = $in{"entrygmembers"};
	@line = split(/\s+/, $entry_local_members);
	$local_members = join(",", @line);
	$not_local_members = &get_nolocal_members($in{"oldname"});

	if($not_local_members && $local_members)
	{
		$umembers = join(",", $local_members, $not_local_members);
	}
	elsif($local_members)
	{
		$umembers = $local_members;
	}
	else
	{
		$umembers = $not_local_members;
	}

	$group{"pass"} = "";
	$group{"group"} = $groupname;
	$group{"gid"} = $ogroup{"gid"};
	$group{"members"} = $umembers;
}

if($error_msg)
{
#	&header($text{'index_title'}, "", undef, 0, 1);
#	print "<p>", $error_msg;
#	&footer("/groups/", $text{'index'});
	if($event)
	{
		&nas_syslog($logminor, $logpriority, $event, $formatstr);
	}

	&error_setup($text{"error_title_modify_group"});

	&error($error_msg);
}
else
{
	&modify_group(\%ogroup, \%group);
	$logminor= $text{'module_name'};
	$logpriority=$text{'naslog_info'};
	$event = $text{'groups_naslog_event_modify_success'};
	$formatstr = sprintf $text{'groups_naslog_modify_success'}, $groupname;	
	&nas_syslog($logminor, $logpriority, $event, $formatstr);
	&redirect("index.cgi");
}

